<?php
/**
 * 日志模型
 * @date    2020-01-01
 * @author  kiro
 * @email   294843009@qq.com
 * @version 1.0
 */

/**
rights Reserved, Designed By www.youyacao.com
@author:成都市一颗优雅草科技有限公司
@version 优雅草蜻蜓系统-服务端部分
注意：服务端代码在获得授权之前通过其他非官方渠道获得代码均为侵权，禁止用于商业用途，否则将承担因此带来等版权纠纷。
需要商业用途或者定制开发等可访问songshu.youyacao.com   联系QQ:2853810243 422108995 23625059584

 */
namespace App\Models;
use Illuminate\Support\Facades\Auth;

class ApiLog extends BaseModel
{
    protected $table = 'api_log';

    protected $fillable = [
        'type',
        'user_id',
        'code',
        'msg',
        'requests',
        'datas',
        'method',
        'action',
        'ip'
    ];

    public static function addLog($type = 0, $code = 200, $msg = '', $datas = '')
    {
        $user = Auth::guard('api')->user();
        $user_id = $user ? $user->id:0;
        return self::create([
            'type'      => $type,
            'user_id'   => $user_id,
            'code'      => $code,
            'msg'       => $msg,
            'datas'     => json_encode($datas),
            'requests'  => json_encode(request()->all()),
            'method'    => request()->method(),
            'action'    => request()->path(),
            'ip'        => request()->ip()
        ]);
    }
}
